package dbutil

import (
	"database/sql/driver"
	"errors"
	"time"
)

type Datetime struct {
	Timestamp
}

type Timestamp struct {
	time.Time
}

func (a *Timestamp) Scan(v any) error {
	t, ok := v.(time.Time)
	if !ok {
		return errors.New("not timestamp/datetime")
	}
	a.Time = t
	return nil
}

func (a Timestamp) Value() (driver.Value, error) {
	// mysql 默认不支持空时间格式
	// Incorrect datetime value: '0000-00-00' for column 'XXX'
	if a.IsZero() {
		return time.Date(1970, 1, 1, 0, 0, 0, 0, time.Local), nil
	}
	return a.Time, nil
}
